package cn.tx.service.impl;

import cn.tx.mapper.PermissionDao;
import cn.tx.mapper.RoleDao;
import cn.tx.mapper.UserDao;
import cn.tx.pojo.Permission;
import cn.tx.pojo.Role;
import cn.tx.pojo.User;
import cn.tx.service.UserService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;

/**
 * @author Snake Eyes
 * @PROJECT_NAME: health_parent
 * @DESCRIPTION:
 * @USER: 11647
 * @DATE: 2021/9/8 19:51
 * @Email: 1164763541@qq.com
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;
    /**
     * @author :11647
     * @date ：Created in 2021/9/9  11:42
     * @description：(描述) 获取用户信息
     * @modified By：11647
     * @version:
     */
    @Override
    public User findByUsername(String username) {
        System.out.println("userservice:"+username);
        User user = userDao.findByUsername(username);
        if (user == null) {
            return null;
        }
        //查询角色信息

        Set<Role> roles = roleDao.findByUserId(user.getId());
        user.setRoles(roles);
        //根据角色查询对应的权限
        for (Role role : roles) {
            Integer roleId = role.getId();
            //查询
            Set<Permission> permission = permissionDao.findByRoleId(roleId);
            role.setPermissions(permission);
        }
        user.setRoles(roles);
        return user;
    }
}
